<?
if (!defined('SMF'))
	die('Hacking attempt...');
	
global $smcFunc, $txt, $scripturl, $user_info;
global $dbtables, $color_header, $color_line1, $color_line2;

// Verify user login, if CheckLogin() returns false return to the calling script/function
if (CheckLogin() == false)
	return;

$title = $txt['bnt_teamplanet_title'];

//-------------------------------------------------------------------------------------------------

$result = $smcFunc['db_query']('', '
	SELECT *
	FROM {raw:db_ships}
	WHERE user_id = {int:user_id}
	LIMIT 1',
	array(
		'db_ships' => $dbtables['ships'],
		'user_id' => $user_info['id'],
	)
);
$playerinfo = $smcFunc['db_fetch_assoc']($result);
$smcFunc['db_free_result']($result);

if ($playerinfo['team'] == 0)
	echo '<br>', $txt['bnt_teamplanet_notally'];
else
{
	BigTitle($title);
	
	$sort = (isset($_GET['sort'])) ? $_GET['sort'] : '';
	
	$query = 'SELECT * FROM ' . $dbtables['planets'] . ' WHERE corp = ' . $playerinfo['team'];
	if (!empty($sort))
	{
		$query .= ' ORDER BY';
		if ($sort == "name")
			$query .= ' ' . $sort . 'ASC';
		elseif ($sort == "organics" || $sort == "ore" || $sort == "goods" || $sort == "energy" ||
				$sort == "colonists" || $sort == "credits" || $sort == "fighters")
			$query .= ' ' . $sort . ' DESC';
		elseif ($sort == "torp")
			$query .= ' torps DESC';
		else
		$query .= ' sector_id ASC';
	}
	
	$result = $smcFunc['db_query']('',
		$query,
		''
	);

	echo '
		<br>
		<b><a href="', $scripturl, '?action=blacknova;sa=planet_report">', $txt['bnt_teamplanet_personal'], '</a></b>
		<br>
		<br>';

	$i = 0;
	if ($smcFunc['db_num_rows']($result) > 0)
	{
		while ($row = $smcFunc['db_fetch_assoc']($result))
		{
			$planet[$i] = $row;
			$i++;
		}
	}

	$num_planets = $i;
	if ($num_planets < 1)
	  echo '<br>', $txt['bnt_teamplanet_noplanet'];
	else
	{
		echo '
		', $txt['bnt_pr_clicktosort'], '<br><br>
		<table width="100%" border="0" cellspacing="0" cellpadding="2">
			<tr bgcolor="', $color_header, '">
				<td><b><a href="', $scripturl, '?action=blacknova;sa=team_planets;sort=sector">', $txt['bnt_sector'], '</a></b></td>
				<td><b><a href="', $scripturl, '?action=blacknova;sa=team_planets;sort=name">', $txt['bnt_name'], '</a></b></td>
				<td><b><a href="', $scripturl, '?action=blacknova;sa=team_planets;sort=ore">', $txt['bnt_ore'], '</a></b></td>
				<td><b><a href="', $scripturl, '?action=blacknova;sa=team_planets;sort=organics">', $txt['bnt_organics'], '</a></b></td>
				<td><b><a href="', $scripturl, '?action=blacknova;sa=team_planets;sort=goods">', $txt['bnt_goods'], '</a></b></td>
				<td><b><a href="', $scripturl, '?action=blacknova;sa=team_planets;sort=energy">', $txt['bnt_energy'], '</a></b></td>
				<td><b><a href="', $scripturl, '?action=blacknova;sa=team_planets;sort=colonists">', $txt['bnt_colonists'], '</a></b></td>
				<td><b><a href="', $scripturl, '?action=blacknova;sa=team_planets;sort=credits">', $txt['bnt_credits'], '</a></b></td>
				<td><b><a href="', $scripturl, '?action=blacknova;sa=team_planets;sort=fighters">', $txt['bnt_fighters'], '</a></b></td>
				<td><b><a href="', $scripturl, '?action=blacknova;sa=team_planets;sort=torp">', $txt['bnt_torps'], '</a></b></td>
				<td><b>', $txt['bnt_base'], '</b></td>
				<td><b>', $txt['bnt_selling'], '</b></td>
				<td><b>', $txt['bnt_player'], '</b></td>
			</tr>';
		
		$total_organics = 0;
		$total_ore = 0;
		$total_goods = 0;
		$total_energy = 0;
		$total_colonists = 0;
		$total_credits = 0;
		$total_fighters = 0;
		$total_torp = 0;
		$total_base = 0;
		$total_selling = 0;
		
		$color = $color_line1;
		
		for ($i = 0; $i < $num_planets; $i++)
		{
			$total_organics += $planet[$i]['organics'];
			$total_ore += $planet[$i]['ore'];
			$total_goods += $planet[$i]['goods'];
			$total_energy += $planet[$i]['energy'];
			$total_colonists += $planet[$i]['colonists'];
			$total_credits += $planet[$i]['credits'];
			$total_fighters += $planet[$i]['fighters'];
			$total_torp += $planet[$i]['torps'];
			
			if ($planet[$i]['base'] == "Y")
				$total_base += 1;
			if ($planet[$i]['sells'] == "Y")
				$total_selling += 1;
			if(empty($planet[$i]['name']))
				$planet[$i]['name'] = "$txt[bnt_unnamed]";
				
			$owner = $planet[$i]['owner'];
			
			$result = $smcFunc['db_query']('', '
				SELECT character_name
				FROM {raw:db_ships}
				WHERE ship_id = {string:owner}',
				array(
					'db_ships' => $dbtables['ships'],
					'owner' => $owner
				)
			);
			$player =  $smcFunc['db_fetch_assoc']($result);
			$smcFunc['db_free_result']($result);

			echo '
			<tr bgcolor="', $color, '">
				<td><a href="', $scripturl, '?action=blacknova;sa=rsmove;engage=1;destination=', $planet[$i]['sector_id'], '">', $planet[$i]['sector_id'], '</a></td>
				<td>', $planet[$i]['name'], '</td>
				<td>', NUMBER($planet[$i]['ore']), '</td>
				<td>', NUMBER($planet[$i]['organics']), '</td>
				<td>', NUMBER($planet[$i]['goods']), '</td>
				<td>', NUMBER($planet[$i]['energy']), '</td>
				<td>', NUMBER($planet[$i]['colonists']), '</td>
				<td>', NUMBER($planet[$i]['credits']), '</td>
				<td>', NUMBER($planet[$i]['fighters']), '</td>
				<td>', NUMBER($planet[$i]['torps']), '</td>
				<td>';
			
			if ($planet[$i]['base'] == 'Y')
				echo $txt['bnt_yes'];
			else
				echo $txt['bnt_no'], '</td>';
			
			if ($planet[$i]['sells'] == 'Y')
				echo '<td>', $txt['bnt_yes'], '</td>';
			else
				echo '<td>', $txt['bnt_no'], '</td>';
			
			echo '
				<td>', $player['character_name'], '</td>
			</tr>';

			if ($color == $color_line1)
				$color = $color_line2;
			else
				$color = $color_line1;
		}
		
		echo '
			<tr bgcolor="', $color, '">
				<td></td>
				<td>', $txt['bnt_pr_totals'], '</td>
				<td>', NUMBER($total_ore), '</td>
				<td>', NUMBER($total_organics), '</td>
				<td>', NUMBER($total_goods), '</td>
				<td>', NUMBER($total_energy), '</td>
				<td>', NUMBER($total_colonists), '</td>
				<td>', NUMBER($total_credits), '</td>
				<td>', NUMBER($total_fighters), '</td>
				<td>', NUMBER($total_torp), '</td>
				<td>', NUMBER($total_base), '</td>
				<td>', NUMBER($total_selling), '</td>
				<td></td>
			</tr>
			</table>';
	}
}

echo '<br><br>';

TextMainMenu();
?>
